<template>
	<view class="page phonegoin">
		<!-- 背景图 -->
		<image class="phonegoin_back" src="https://newbeaconyt.oss-cn-hangzhou.aliyuncs.com/app/daojia/common/theball.png"></image>
		<image class="phonegoin_ball" src="https://newbeaconyt.oss-cn-hangzhou.aliyuncs.com/app/daojia/common/ball.png"></image>
		<!-- 登录模块 -->
		<view class="modular">
			<input class="input_mobile" placeholder="请输入您的手机号码" type="number" @input="Onmobile" placeholder-class="placeholderClass" />
			<!-- 验证码 -->
			<view class="verification_bos">
				<input class="input_captcha" @input="Oncode" placeholder="请输入验证码" type="number" placeholder-class="placeholderClass" />
				<button class="verification" :disabled="wait > 0" @click.stop="getVerifyCode">
					<text v-if="wait > 0">等待{{wait}}秒</text>
					<text v-else>获取验证码</text>
				</button>
			</view>
			<!-- 登陆按钮 -->
			<button type="default" class="custom_class" @click='submit'>登录</button>
			
			<view class="tips font-969696" @click="goNext('/pages/common/update?mode=create')">还未注册？<text class="font-link">去注册</text></view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				mobile: '', //手机号码
				code: '', //验证码
				wait: 0, // 验证码等待的时间
				timer: null,
				idInfo: {}
			}
		},
		onLoad: function(options) {
			// 从缓存中拿取各种id信息
			this.idInfo = uni.getStorageSync('idInfo')
		},
		methods: {
			// 手机号码
			Onmobile(e) {
				this.mobile = e.detail.value
			},
			// 验证码
			Oncode(e) {
				this.code = e.detail.value
			},
			// 获取手机验证码
			getVerifyCode () {
				if (this.$base.checkMobile(this.mobile)) {
					this.$Http({
						url: 'sendCode',
						data: {
							mobile: this.mobile,
							event: 'login'
						},
					}).then(res => {
						if (res.code == 1) {
							uni.showToast({ title: '验证码发送成功' })
							this.wait = 60
							this.timer = setInterval(() => {
								this.wait--
								if (this.wait === 0) {
									clearInterval(this.timer)
								}
							}, 1000)
						} else {
							uni.showToast({ title: res.msg, icon: 'none' })
						}
					});
				} else {
					uni.showToast({ title: '请输入正确的手机号码', icon: 'none' })
				}
			},
			// 登录
			submit() {
				var that = this
				if (!this.$base.checkMobile(this.mobile)) {
					uni.showToast({ title: '请输入正确的手机号码', icon: 'none' })
					return
				}
				if (this.code == '') {
					wx.showToast({ title: '验证码不能为空', icon: 'none' })
					return
				}
				this.$Http({
					url: 'phonelogin',
					type: 'POST',
					load: true,
					data: {
						mobile: that.mobile,
						code: that.code
					},
				}).then(res => {
					if (res.code == 1) {
						uni.setStorage({
							key: 'token',
							data: res.data.token,
							success() {
								// 登录完成后获取用户信息
								that.getUserInfo()
							}
						})
					} else if (res.code == 10) {
						uni.showModal({
							title: '提示',
							confirmText: '确定',
							confirmColor: '#03004c',
							cancelText: '取消',
							content: '您还未注册，请先注册',
							success: function(res) {
								if (res.confirm) {
									uni.navigateTo({ url: '/pages/common/update?mode=create' });
								}
							}
						});
					} else {
						uni.showToast({  title: res.msg, icon: 'none' });
					}
				});
			},
			// 获取用户信息
			getUserInfo () {
				this.$Http({
					url: 'user',
					token: true,
					load: true
				}).then(res => {
					if (res.code == 1) {
						let obj = {};
						// 员工分享的父id是自己，会员分享的父id是终极父id
						let type = res.data.user.group
						if (type.indexOf('员工') >= 0) {
						  obj.Pid = res.data.user.user_id
						} else if (type.indexOf('会员') >= 0) {
						  obj.Pid = res.data.user.parent_id
						}
						
						// 分享出去的来源id永远是自己的id
						obj.Fid = res.data.user.user_id;
						
						let $C = this.idInfo.Cid;
						if ($C) {
							obj.Cid = $C
						}
						
						uni.setStorage({key: 'idInfo', data: obj })
						uni.setStorage({key: 'userInfo', data: res.data})
						
						this.$base.Onpageurl()
					}
				});
			},
			// 路由跳转
			goNext(url) {
				uni.navigateTo({ url: url });
			}
		}
	}
</script>

<style lang="less">
	@import url("style.less");
</style>
